package com.bcts.rrh.drawingboard.shapeview;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.Region;

import com.bcts.rrh.drawingboard.Constants;

public  class Pathview {

    public Paint paint;
    public Path path;
    public Point point1,point2,point3;//绘制三角形的三个点坐标,三角形专用
    private int type=0;//记录path的,一共四种模式路径
    private static final int STROKE_TYPE_DRAW=2;//画笔模式
    private static final int STROKE_TYPE_LINE = 3;//划线模式
    private static final int STROKE_TYPE_CIRCLE = 4;//画圆模式
    private static final int STROKE_TYPE_triangle = 5;//画三角形形模式
    private static final int STROKE_TYPE_RECTANGLE = 6;//画矩形模式

    float x1,y1,x2,y2,x3,y3,x4,y4;//图形移动时需要用到
    Matrix mMatrix ;
    private float mAngle=0;//旋转的角度
    private float mOldAngle=0;//旋转的角度
    private float mAnglePi=0;
    float rotateStartX,rotateStartY,rotateX,rotateY;//rotateStartX,rotateStartY是旋转的起点，rotateX，rotateY是旋转中心点
    public Bitmap bitmap;
    public boolean moveable=false;//Pathview对象是否可以移动
    private Region region=new Region();

    public Pathview() {
        path=new Path();
        x1=0;x2=0;x3=0;y1=0;y2=0;y3=0;
        paint = new Paint();
        paint.setColor(Constants.penColor);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.STROKE);//设置空心
    }

    /**
     * 记录PathView代表哪一种图形
     * @param type
     */
    public void setType(int type){
        this.type=type;
    }

    /**
     *画直线的函数
     * @param startX 起始点的X
     * @param startY 起始点的Y
     * @param endX 终止点的X
     * @param endY 终止点的Y
     */
    public void linepath(float startX,float startY,float endX,float endY){
        //用内部变量记录直线的初始位置
        x1=startX;
        y1=startY;
        x2=endX;
        y2=endY;

        path=new Path();
        path.moveTo(startX,startY);
        path.lineTo(endX,endY);

    }

    /**
     * 画椭圆
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    public void ovalpath(float left, float top, float right, float bottom){
        //用内部变量记录椭圆的初始位置
        x1=left;
        y1=top;
        x2=right;
        y2=bottom;
        rotateX = (x1 + x2) / 2;//中心点
        rotateY = (y1 + y2) / 2;
        path=new Path();
        RectF rectF=new RectF(left,top,right,bottom);
        path.addOval(rectF,Path.Direction.CW);
        mMatrix=new Matrix();
        mMatrix.postRotate(mAngle,rotateX,rotateY);//以x轴为中线进行翻转
        // mMatrix.postRotate(mAngle);//以坐标轴原点为中心点顺时针旋转mAngle°
        path.transform(mMatrix);

    }

    /**
     * 画三角形的函数，根据三个顶点画
     */
    public void tripath(){
        path=new Path();
        path.moveTo(point1.x,point1.y);
        path.lineTo(point2.x,point2.y);
        path.lineTo(point3.x,point3.y);
        path.close();
        //求重心坐标
        rotateX=((point1.x)+(point2.x)+(point3.x))/3;
        rotateY=((point1.y)+(point2.y)+(point3.y))/3;
        //旋转的角度
        mMatrix=new Matrix();
        mMatrix.postRotate(mAngle,rotateX,rotateY);//以x轴为中线进行翻转
        // mMatrix.postRotate(mAngle);//以坐标轴原点为中心点顺时针旋转mAngle°
        path.transform(mMatrix);
    }

    /**
     * 画矩形的函数，根据四条边画矩形
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    public void rectpath(float left, float top, float right, float bottom){
        //用内部变量记录椭圆的初始位置
        x1=left;
        y1=top;
        x2=right;
        y2=bottom;
        rotateX = (x1 + x2) / 2;
        rotateY = (y1 + y2) / 2;
        path=new Path();
        path.addRect(left,top,right,bottom,Path.Direction.CW);

        mMatrix=new Matrix();//初始化旋转矩阵
        mMatrix.postRotate(mAngle,rotateX,rotateY);//以x轴为中线进行翻转
        // mMatrix.postRotate(mAngle);//以坐标轴原点为中心点顺时针旋转mAngle°
        path.transform(mMatrix);
        //path.close();
    }

    public void drawview(Canvas canvas){
        if(path!=null){//如果线不为空，则在画布上画线
            canvas.drawPath(path,paint);
        }
        if(bitmap!=null){//如果bitmap不为空，则在画布上画bitmap
            canvas.drawBitmap(bitmap,0,0,paint);
        }

    }

    public void Contains(float eventX,float eventY){//判断触点是否在三角形内部
        //构造一个区域对象,左闭右开的
        RectF r=new RectF();
        //计算控制点的边界
        path.computeBounds(r,true);
        //设置区域路径和剪辑描述的区域
        region.setPath(path, new Region((int)r.left,(int)r.top,(int)r.right,(int)r.bottom));
        //判断点是否在范围内
        moveable=region.contains((int)eventX,(int)eventY);
    }

    public void setFill(int color){
        paint.setStyle(Paint.Style.FILL);//设置实心
        paint.setColor(color);
    }

    /**
     * 记录移动开始时的初始信息
     * @param x
     * @param y
     */
    public void movestrat(float x,float y){
        if(moveable==true){
            switch (type){
                case STROKE_TYPE_CIRCLE://椭圆
                    x3=x-x1;
                    y3=y-y1;
                    x4=x-x2;
                    y4=y-y2;
                    break;
                case STROKE_TYPE_triangle://三角形
                    if( point1!=null){
                        x1=x-point1.x;
                        y1=y-point1.y;
                        x2=x-point2.x;
                        y2=y-point2.y;
                        x3=x-point3.x;
                        y3=y-point3.y;
                    }
                    break;
                case STROKE_TYPE_RECTANGLE://矩形
                    x3=x-x1;
                    y3=y-y1;
                    x4=x-x2;
                    y4=y-y2;
                    break;
                case STROKE_TYPE_DRAW://画笔
                    x1=x;
                    y1=y;
                    break;
            }
        }
    }

    public void moveView(float x,float y){
        if(moveable==true){
            switch (type){
                case STROKE_TYPE_CIRCLE://椭圆
                    ovalpath(x-x3,y-y3,x-x4,y-y4);
                    break;
                case STROKE_TYPE_triangle://三角形
                    if(point1!=null){
                        point1=new Point((int)(x-x1),(int)(y-y1));
                        point2=new Point((int)(x-x2),(int)(y-y2));
                        point3=new Point((int)(x-x3),(int)(y-y3));
                        tripath();
                    }
                    break;
                case STROKE_TYPE_RECTANGLE://矩形
                    rectpath(x-x3,y-y3,x-x4,y-y4);
                    break;
                case STROKE_TYPE_DRAW://移动画笔
                    path.offset(x-x1,y-y1);
                    x1=x;
                    y1=y;
                    break;
            }
        }
    }

    public void rotateViewPressed(float x,float y){//开始手按住图形的时候
        if(moveable==true) {
            //开始转动时记录起始的坐标
            rotateStartX = x;
            rotateStartY = y;
            //寻找转动的中心点
            switch (type) {//根据View类型计算图形的中心点，即旋转点
                case STROKE_TYPE_CIRCLE://椭圆的中心点即圆心
                    rotateX = (x1 + x2) / 2;
                    rotateY = (y1 + y2) / 2;
                    //angle=getAngle(rotateX,rotateY,rotateStartX,rotateStartY);
                    break;
                case STROKE_TYPE_RECTANGLE://矩形的中心点即中心位置
                    rotateX = (x1 + x2) / 2;
                    rotateY = (y1 + y2) / 2;
                    break;
                case STROKE_TYPE_triangle://三角形中心点需要根据3顶点坐标进行计算
                    rotateX=((point1.x)+(point2.x)+(point3.x))/3;
                    rotateY=((point1.y)+(point2.y)+(point3.y))/3;
                    break;

            }
        }
    }



    public void rotateViewMove(float x,float y){//旋转时手的移动
        switch (type){//根据View类型计算图形的中心点，即旋转点
            case STROKE_TYPE_CIRCLE://椭圆的中心点即圆心
                updateRotate(x,y);
                ovalpath(x1,y1,x2,y2);
                //angle=getAngle(rotateX,rotateY,rotateStartX,rotateStartY);
                break;
            case STROKE_TYPE_RECTANGLE://矩形的中心点即中心位置
                updateRotate(x,y);
                rectpath(x1,y1,x2,y2);
                break;
            case STROKE_TYPE_triangle://三角形中心点需要根据3顶点坐标进行计算
                updateRotate(x,y);
                tripath();
                break;
        }
    }


    private void updateRotate(double endX,double endY) {

        mAnglePi = (float) Math.atan((Math.abs(endY - rotateY))
                / Math.abs((endX - rotateX)));
        mAngle = (float) ((180 * mAnglePi) / (Math.PI));
        if (rotateX < endX && rotateY>= endY) {// 第一象限
            mAngle = -mAngle;
            mAnglePi = -mAnglePi;
            //System.out.println("1mAngle="+mAngle+",mAnglePi="+mAnglePi);

        } else if (rotateX >= endX && rotateY > endY) {// 第二象限
            mAngle += 180;
            mAnglePi += Math.PI;

            //System.out.println("2mAngle="+mAngle+",mAnglePi="+mAnglePi);

        } else if (rotateX > endX && rotateY <= endY) {// 第三象限
            mAngle = 180 - mAngle;
            mAnglePi = (float) (Math.PI - mAnglePi);

            //System.out.println("3mAngle="+mAngle+",mAnglePi="+mAnglePi);
        }else {
            //System.out.println("4mAngle="+mAngle+",mAnglePi="+mAnglePi);
        }

    }

}
